/*
 * @Author: decadent_zq 9867470+decadent-zq@user.noreply.gitee.com
 * @Date: 2023-03-31 15:57:59
 * @LastEditors: decadent_zq 9867470+decadent-zq@user.noreply.gitee.com
 * @LastEditTime: 2023-04-01 10:02:17
 * @FilePath: \absolutuniq_pc\src\main.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import SlideVerify from 'vue-monoplasty-slide-verify';
// 引入生成二维码插件
import QRCode from "qrcode"
import VueAMap from 'vue-amap'
import moment from 'moment'
import Base64 from './utils/base64.js'
import Router from 'vue-router'


Vue.prototype.$Base64 = Base64;

moment.locale('zh-cn')
Vue.prototype.$moment = moment

Vue.prototype.$QRCode = QRCode
// 引入 Element
Vue.use(ElementUI)

Vue.use(SlideVerify)

// 引入 axios
Vue.prototype.$axios = axios

// websocket 连接url
// const ws = 'ws://127.0.0.1:8088'
const ws = 'ws://101.43.223.224:16981'

// axios请求url前缀
axios.defaults.baseURL = 'http://127.0.0.1:8088'
// axios.defaults.baseURL = 'http://101.43.223.224:16981'

// 图片回显统一路径
const imageUrl = 'http://101.43.223.224:8765/image/getImageById'

const mainColor = 'red'

Vue.prototype.$ws = ws
Vue.prototype.$imageUrl = imageUrl
Vue.prototype.$mainColor = mainColor
Vue.config.productionTip = false
// 高德地图
Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
  key: '16146368eb45c80bfeef1913f4c128ae',  //上面步骤提到的key复制过来
  plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
  v: '1.4.4'
});

axios.interceptors.request.use((config) => {
  config.headers.Authorization_protal = window.sessionStorage.getItem('token')
  return config
})

axios.interceptors.response.use((config) => {
  if(config.data.code === 20003
     || config.data.code === 20004 
     || config.data.code === 20008 
     || config.data.code === 20011 
     || config.data.code === 20012
     || config.data.code === 20001
     || config.data.code === 20005
     || config.data.code === 20009
     || config.data.code === 20007
     || config.data.code === 100022
     ){
      window.sessionStorage.removeItem('token')
      window.sessionStorage.removeItem('userId')
      let response = config.request.responseURL
      console.log(response)
      router.push('/Login')
  } 
  if(config.data.code !== 200){
      if(config.data.data.errCode === 20011){
        window.sessionStorage.removeItem('token')
        window.sessionStorage.removeItem('userId')
        window.sessionStorage.removeItem('avatarUrl')
        location.reload() 
      }
  }
  return config
})

Vue.prototype.setSessionItem = function (key,) {
  // 创建 StorageEvent 事件
  let newStorageEvent = document.createEvent("StorageEvent");
  const storage = {
    setItem: function (k,) {
      sessionStorage.setItem(k, val);

      // 初始化 StorageEvent 事件
      newStorageEvent.initStorageEvent(
        "setItem", // 事件别名
        false,
        false,
        k,
        null,
        val,
        null,
        null
      );
      // 派发事件
      window.dispatchEvent(newStorageEvent);
    },
  };
  return storage.setItem(key, newVal);
};

// 解决vue-router@3.0版本及以上回调形式已经改成promise api的形式了
// 返回的是一个promise，如果路由地址跳转相同, 且没有捕获到错误，
// 3.0以下版本则不会出现以下警告！！！，因路由回调问题…
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
